<?php
namespace app\jobmanagement\service;

use app\model\TaskTask as TaskModel;
use app\model\TaskWork as TaskWorkModel;
use app\model\Capital as CapitalModel;
class Chart

{
    /************************* 图表方法 start **************************/

    private $mTaskTask;
    private $mCapital;
    private $mTaskWork;
    private $yesterday;
    public function __construct(){
        $this->mTaskTask = new TaskModel();
        $this->mTaskWork = new TaskWorkModel();
        $this->mCapital = new CapitalModel();
        $this->yesterday = date("Y-m-d", strtotime("-1 day"));
        // 2020-06-15 07:00:00 前使用测试数据
        if (time() < 1592262000) {
            $this->yesterday = '2020-06-12';
        }
    }


    /**
     * @introduction:返回所有生产车间今日所支付的计件费用
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author:cxh
     * @date: 2020/6/15 17:31
     */
    public function todayAmount(){
        $plant_area = config('dic.plant_area');
        //查询今天对应的数据
        $taskList = $this->mTaskTask
            ->field('dept_id,task_amount')
            ->where('task_date' , $this->yesterday)
            ->where('task_status', '<>', 99)
            ->select();
        $taskList = collection($taskList)->toArray();

        $group = [];
        foreach ($taskList as $value) {
            $group[$value['dept_id']][] = $value['task_amount'];
        }

        $res = [];
        foreach ($plant_area as $key => $va){
            $amount = @$group[$key];
            $all_amount = !empty($amount) ? array_sum($amount) : 0;

            $res[] = [
                'id' => $key,
                'name' => $va,
                'amount' => $all_amount
            ];
        }

        return $res;
    }


    /**
     * @introduction:从任务单形成数据中读取今天使用人数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author:cxh
     * @date: 2020/6/15 17:13
     */
    public function userCount(){
        $plant_area = config('dic.plant_area');
        //查询昨天对应的数据
        $userList = $this->mTaskTask
            ->alias('tt')
            ->field('tt.dept_id,tw.worker')
            ->join('task_work tw','tt.id = tw.task_id','right')
            ->where('tt.task_date' , $this->yesterday)
            ->where('tt.task_status', '<>', 99)
            ->select();
        $group = [];
        foreach ($userList as $value) {
            $group[$value['dept_id']][] = $value['worker'];
        }

        $res = [];
        foreach ($plant_area as $key => $va){
            $worker = @$group[$key];
            $num = !empty($worker) ? count(array_unique(array_filter(explode('-',implode('-',$worker))))) : 0;

            $res[] = [
                'id' => $key,
                'name' => $va,
                'num' => $num
            ];
        }

        return $res;
    }



    /**
     * @auther zlq
     * @create_time 2020/6/15 14:23
     * @description 获取今天实时任务单数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getTodayFinishTask()
    {
        $task = $this->mTaskTask->where(['task_date' => $this->yesterday, 'task_status' => 6])
            ->select();
        if (empty($task)) {
            return [];
        } else {
            // 获取部门数据
            $dept = config("dic.plant_area");
            $temp = [];
            foreach ($task as $one) {
                $temp[] = [
                    'prod_name' => $one['prod_name'],
                    'prod_amount' => $one['prod_amount'],
                    'prod_unit' => $one['prod_unit'],
                    'prod_number' => floatval($one['prod_number']),
                    'task_worker_amount' => $one['task_worker_amount'],
                    'task_amount' => $one['task_amount'],
                    'per_worker_amount' => sprintf("%.2f", ($one['task_amount'] / $one['task_worker_amount'])),
                    'dept_name' => $dept[$one['dept_id']],
                ];
            }
            return $temp;
        }
    }

    /**
     * @auther zlq
     * @create_time 2020/6/15 15:46
     * @description 获取上周数据
     * @return array[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLastWeekData()
    {
        $today = $this->yesterday;
        $last_week = [];
        $people = [];
        $money = [];
        for ($m = 6; $m > -1; $m--) {
            if ($m != 0) {
                $temp_day = date('Y-m-d', strtotime($today . ' -'. $m . ' day'));
            } else {
                $temp_day = $today;
            }
            $last_week[] = date('m-d', strtotime($temp_day));
            // 计算当天使用人数和当天总工资
            $task = $this->mTaskTask->where(['task_date' => $temp_day])
                ->where('task_status', '<>', 99)
                ->select();
            if (empty($task)) {
                $people[] = 0;
                $money[] = 0;
            } else {
                $temp_money = 0;
                $temp_people_str = '';
                foreach ($task as $one) {
                    $temp_money += $one['task_amount'];
                    $user = $this->mTaskWork->field('worker')
                        ->where(['task_id' => $one['id']])
                        ->select();
                    if (!empty($user)) {
                        foreach ($user as $two) {
                            $temp_people_str .= $two['worker'];
                        }
                    }
                }
                $temp_people = explode('-', trim($temp_people_str, '-'));
                $people[] = count(array_unique(array_filter($temp_people)));
                $money[] =  (float)sprintf($temp_money, '%.2f');
            }
        }
        return ['time' => $last_week, 'people' => $people, 'money' => $money];
    }
    /************************* 图表方法   end **************************/

    /**
     * @auther zlq
     * @create_time 2020/5/21 22:32
     * @description 获取导出表格数据
     * @param $param
     * @param $type
     * @return array|bool|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCapitalListExcel($param, $type)
    {
        $select = ['create_time' => ['like', $this->yesterday.'%']];
        switch ($type) {
            case 1:
                $ids = $param['ids'];
                $ids_array = array_filter(explode('-', $ids));
                if (empty($ids_array)) {
                    return [];
                }
                $select['id'] = ['in', $ids_array];
                return $this->mCapital
                    ->where($select)
                    ->order('id desc')
                    ->select();
                break;
            case 2:
                if($param['user']){
                    $select['dd_user_id'] = $param['user'];
                }
                if($param['capital_status']){
                    $select['status'] = $param['capital_status'];
                }
                if($param['capital_type']){
                    $select['type'] = $param['capital_type'];
                }
                return $this->mCapital
                    ->where($select)
                    ->order('id desc')
                    ->select();
                break;
            default:
                return [];
                break;
        }

    }
}